﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Odyssey.Consul;
using System;
namespace Athena.Consul
{
    using Options;
    using RouteAddress;
    using Serialization;
    public static class AthenaConsulExtension
    {
        public static IServiceCollection AddConsulRouteManager(this IServiceCollection serviceCollection, Action<IConsulConfiguration> actionBuildConsulConfiguration, Action<ConsulRouteAddressOption> actionBuildConsulRouteAddressOption)
        {
            var consulRouteAddressOption = new ConsulRouteAddressOption();
            actionBuildConsulRouteAddressOption.Invoke(consulRouteAddressOption);
            serviceCollection.AddConsul(actionBuildConsulConfiguration);
            serviceCollection.AddSingleton<IServiceMultipleRouteManager>((provider) =>
            {
                return new ConsulMultipleRouteManager(
                    provider.GetRequiredService<ConsulDispatch>(),
                    provider.GetRequiredService<ServiceDescriptor>(),
                    provider.GetRequiredService<ISerializer<string>>(),
                    provider.GetRequiredService<IRouteDescriptorChanger>(),
                    provider.GetRequiredService<ILogger<ConsulMultipleRouteManager>>(),
                    provider.GetRequiredService<RuntimeServiceOption>(),
                    consulRouteAddressOption
                    );
            });
            return serviceCollection;
        }
    }
}
